package com.mrent.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.mrent.entity.Order;
import com.mrent.util.Utils;

@Component
public class OrderServiceImpl implements OrderService {
	
	/**
	 * 保存订单.
	 * 
	 * @param order
	 */
	public void saveOrder(Order order){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try{
			trans.begin();
			em.persist(order);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			trans.commit();
		}
	}
	
	/**
	 * 修改订单.
	 * 
	 * @param order
	 */
	public Order getOrder(Long  orderId){
		EntityManager em = EMF.getEM();
		Order order = null;
		try {
			order = em.find(Order.class, orderId);
		}catch(Exception e){
			e.printStackTrace();
		}
		return order;
	}
	
	/**
	 * 获取订单.
	 * 
	 * @param order
	 */
	public Order getOrder(String  orderNo){
		EntityManager em = EMF.getEM();
		Order order = null;
		try {
			String jql = "select from Order order where order.orderNo=:orderNo";
			Query query = em.createQuery(jql);
			query.setParameter("orderNo", orderNo);
			order = (Order)query.getSingleResult();
		}catch(Exception e){
			e.printStackTrace();
		}
		return order;
	}
	
	/**
	 * 修改订单.
	 * 
	 * @param order
	 */
	public void updateOrder(Order order){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try {
			trans.begin();
			em.merge(order);
		}catch(Exception e){
			e.printStackTrace();
		}
		finally {	
			trans.commit();
		}
	}
	
	/**
	 * 删除申请订单.
	 * 
	 * @param orderId
	 */
	public void delOrder(Long orderId){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try {
			trans.begin();
			Order order = em.find(Order.class, orderId);
			if(order != null){
				em.remove(order);
			}
		}catch(Exception e){
			System.err.println("异常问题");
			e.printStackTrace();
		}
		finally {
			trans.commit();
		}
	}
	
	/**
	 * 订单列表.
	 * 
	 * @param page
	 * @return
	 */
	public List<Order> listOrder(Page page){
		EntityManager em = EMF.getEM();
		String jpql = "select from Order o ";
		Query query = em.createQuery(jpql);
		page.setTotalCount(query.getResultList().size());
		query.setFirstResult(page.getFirst());
		query.setMaxResults(page.getPageSize());
		List<Order> list = query.getResultList();
		return list;
	}
	
	/**
	 * 根据日期查出相同开店日期、到期日期的订单.
	 * 
	 * @param year
	 * @param month
	 * @param day
	 * @return
	 */
	public List<Order> listOrderBySameDate(Integer year,Integer month,Integer day,Integer endYear,Integer endMonth,Integer endDay){
		
		EntityManager em = EMF.getEM();
		
		String jpql = "select from Order o where o.orderRequestYear = :year and o.orderEndYear = :endYear and o.orderRequestMonth = :month " +
		"and o.orderEndMonth = :endMonth and o.orderRequestDay = :day  and o.orderEndDay = :endDay and o.orderState";
		
		
		Query query = em.createQuery(jpql);
		query.setParameter("year", year);
		query.setParameter("month", month);
		query.setParameter("day", day);
		query.setParameter("endYear", endYear);
		query.setParameter("endMonth", endMonth);
		query.setParameter("endDay", endDay);
		List<Order> list = query.getResultList();
		
		return list;
	}
	
	
	
	/**
	 * 根据日期查出介于开店日期、到期日期之间的订单.
	 * 
	 * @param year
	 * @param month
	 * @param day
	 * @return
	 */
	public List<Order> listOrderByDate(Integer year,Integer month,Integer day,Integer endYear,Integer endMonth,Integer endDay){
		
		EntityManager em = EMF.getEM();
		
		/*String jpql = "select from Order o where o.orderRequestYear <= :year and o.orderEndYear > :year and o.orderRequestMonth <= :month " +
		"and o.orderEndMonth > :month and o.orderRequestDay <= :day  and o.orderEndDay > :day and o.orderState < 3";*/
		
		String jpql = "select from Order o " +
				"where o.orderState < 3 ";
		Query query = em.createQuery(jpql);
		/*query.setParameter("year", year);
		query.setParameter("month", month);
		query.setParameter("day", day);*/
		List<Order> list = query.getResultList();
		List<Order> bakList = new ArrayList<Order>();
		//日期构造.
		Calendar calendarstart = Calendar.getInstance();
		calendarstart.set(Calendar.YEAR, year);
		calendarstart.set(Calendar.MONTH, month);
		calendarstart.set(Calendar.DATE, day);
		Calendar calendarend = Calendar.getInstance();
		calendarend.set(Calendar.YEAR, endYear);
		calendarend.set(Calendar.MONTH, endMonth);
		calendarend.set(Calendar.DATE, endDay);
		for(Order order:list){
			Calendar dbcalendarstart = Calendar.getInstance();
			dbcalendarstart.set(Calendar.YEAR, order.getOrderRequestYear());
			dbcalendarstart.set(Calendar.MONTH, order.getOrderRequestMonth());
			dbcalendarstart.set(Calendar.DATE, order.getOrderRequestDay());
			Calendar dbcalendarend = Calendar.getInstance();
			dbcalendarend.set(Calendar.YEAR, order.getOrderEndYear());
			dbcalendarend.set(Calendar.MONTH, order.getOrderEndMonth());
			dbcalendarend.set(Calendar.DATE, order.getOrderEndDay());
			if(Utils.orderCalendarComp(calendarstart,calendarend,dbcalendarstart,dbcalendarend)){
				bakList.add(order);
			}
		}
		return bakList;
	}
	
	/**
	 * 取用户申请确认订单.
	 * 
	 * @param memberName
	 * @return
	 */
	public List<Order> getOrderByUser(String memberName){
		EntityManager em = EMF.getEM();
		String jpql = "select from Order o where o.orderState < 3 and o.orderMember = :memberName";
		
		Query query = em.createQuery(jpql);
		query.setParameter("memberName", memberName);
		List<Order> orders = query.getResultList();
		return orders;
	}
	
	/**
	 * 取用户申请确认订单,加分页.
	 * 
	 * @param memberName
	 * @return
	 */
	public List<Order> getOrderByUser(String memberName,Page page){
		EntityManager em = EMF.getEM();
		String jpql = "select from Order o where o.orderState < 3 and o.orderMember = :memberName";
		
		Query query = em.createQuery(jpql);
		query.setParameter("memberName", memberName);
		page.setTotalCount(query.getResultList().size());
		query.setFirstResult(page.getFirst());
		query.setMaxResults(page.getPageSize());
		List<Order> orders = query.getResultList();
		return orders;
	}
	
	
	
	

}
